//
//  ICRedPacketResultViewController.m
//  ICome
//
//  Created by 席鹏飞 on 2016/11/8.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICRedPacketResultViewController.h"
#import "ICRedPacketResultHeaderView.h"
#import "ICRedPacketResultTableViewCell.h"
#import "ICClickRedPacketModel.h"
#import "ICRedPacketNetworkManager.h"
#import "ICHeaderFile.h"

@interface ICRedPacketResultViewController () <UITableViewDelegate, UITableViewDataSource, ICRedPacketResultHeaderViewDelegate>
@property (nonatomic, strong) ICRedPacketResultHeaderView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ICClickRedPacketModel *model;
@property (nonatomic, copy) NSString *sectionHeaderString;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, assign) NSInteger sendTime;
@end

@implementation ICRedPacketResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view

    [self getData];
    [self setUI];
    [self setNav];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavBackgroundColor:NE_REDPACKET_BACKGROUND_COLOR];
    [self setNavShadowColor:NE_REDPACKET_BACKGROUND_COLOR];
}

#pragma mark - private methods

- (void)getData {
    self.model = self.parameterDictionary[@"model"];
    self.headerView.model = self.parameterDictionary[@"model"];
    NSString *gId = self.parameterDictionary[@"groupId"];
    self.sendTime = [self.parameterDictionary[@"sendTime"] integerValue];
    
    //领取人按时间排序
    [self sortByTime];
    
    NSString *pageNo = self.model.pageNo;
    CGRect rect = self.headerView.frame;
    if ([pageNo intValue] == 24) {
//        rect.size.height = App_Frame_Width / 375.0 * 132;
        rect.size.height = 132;
        self.headerView.frame = rect;
        self.sectionHeaderString = [NSString stringWithFormat:@"红包金额%@分，等待对方领取", self.model.sum];
    }else if ([pageNo intValue] == 18) {
        rect.size.height = 222;
        self.headerView.frame = rect;
    }else if ([pageNo intValue] == 26) {
        rect.size.height = 132;
        self.headerView.frame = rect;
        self.sectionHeaderString = [NSString stringWithFormat:@"1个红包共%@分", self.model.sum];
    }else if ([pageNo intValue] == 2 || [pageNo intValue] == 13) {
        rect.size.height = 222;
        self.headerView.frame = rect;
        self.sectionHeaderString = [NSString stringWithFormat:@"已领取%@/%@，共%@分/%@分", self.model.getCount, self.model.sumCount, self.model.getSum, self.model.sum];
    }else if ([pageNo intValue] == 25) {
        rect.size.height = 132;
        self.headerView.frame = rect;
        self.sectionHeaderString = [NSString stringWithFormat:@"已领取%@/%@，共%@分/%@分", self.model.getCount, self.model.sumCount, self.model.getSum, self.model.sum];
    }else if ([pageNo intValue] == 10) {
        if ([gId containsString:@"dbl"]) {
            rect.size.height = 222;
            self.headerView.frame = rect;
        }else {
            rect.size.height = 222;
            self.headerView.frame = rect;
            self.sectionHeaderString = [NSString stringWithFormat:@"已领取%@/%@，共%@分/%@分", self.model.getCount, self.model.sumCount, self.model.getSum, self.model.sum];
        }
    }
    
    
    NSDate *sendDate = [NSDate dateWithTimeIntervalSince1970:_sendTime];
    if ([pageNo intValue] == 26) {
        if (self.model.sum.length > 0) {
            ICClickRedPacketListModel *listModel = (ICClickRedPacketListModel *)self.model.list[0];
            NSDate *overDate = [NSDate dateWithTimeIntervalSince1970:[listModel.createDate integerValue]];
            self.sectionHeaderString = [NSString stringWithFormat:@"%@%@被抢光", self.sectionHeaderString, [self getOverTimeWithSendDate:sendDate overDate:overDate]];
        }
    }else if ([pageNo intValue] == 2 || [pageNo intValue] == 13 || [pageNo intValue] == 25 || [pageNo intValue] == 10) {
        if ([self.model.getCount isEqualToString:self.model.sumCount]) {
            ICClickRedPacketListModel *listModel = (ICClickRedPacketListModel *)self.dataList[0];
            NSDate *overDate = [NSDate dateWithTimeIntervalSince1970:[listModel.createDate integerValue]];
            self.sectionHeaderString = [NSString stringWithFormat:@"%@，%@被抢光", self.sectionHeaderString, [self getOverTimeWithSendDate:sendDate overDate:overDate]];
        }
    }
    
    [self.tableView reloadData];
}

// 设置页面布局
- (void)setUI {

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}
// 设置navigation
- (void)setNav {
    self.title = @"新奥红包";
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamedFromiComeSDKBundle:@"App_back_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)]];
}

- (void)leftButtonAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sortByTime {
    self.dataList = [self.model.list sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        ICClickRedPacketListModel *listModel1 = (ICClickRedPacketListModel *)obj1;
        ICClickRedPacketListModel *listModel2 = (ICClickRedPacketListModel *)obj2;
        NSComparisonResult result = [listModel1.createDate compare:listModel2.createDate];
        return result == NSOrderedAscending;
    }];
}

- (NSString *)getOverTimeWithSendDate:(NSDate *)sendDate overDate:(NSDate *)overDate {
    NSTimeInterval timeInterval = [overDate timeIntervalSinceDate:sendDate]/1000;
    if (timeInterval > 3600) {
        return [NSString stringWithFormat:@"%.1f小时", timeInterval / 3600];
    } else if (timeInterval > 60) {
        return [NSString stringWithFormat:@"%.f分钟", timeInterval / 60];
    } else {
        return [NSString stringWithFormat:@"%.f秒", timeInterval];
    }
}

#pragma mark - ICRedPacketResultHeaderViewDelegate

- (void)myPacketBtnClicked:(id)sender {
//    UIViewController *viewController = [[IComMediator sharedInstance] IComMediator_RedPacketViewController:nil];
//    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 37;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, tableView.width-30, 25)];
    label.backgroundColor = [UIColor clearColor];
    label.text = self.sectionHeaderString;
    label.textColor = [UIColor lightGrayColor];
    label.font = ICFont(12.0 * Level);
    [view addSubview:label];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 68;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"RedPacketResultCell";
    ICRedPacketResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[ICRedPacketResultTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.model = self.dataList[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Getter

- (ICRedPacketResultHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ICRedPacketResultHeaderView alloc] initWithFrame:CGRectZero];
        _headerView.delegate = self;
    }
    return _headerView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.userInteractionEnabled = YES;
        _tableView.backgroundColor = BACKGROUNDCOLOR;
        _tableView.tableHeaderView = self.headerView;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
